package com.qy.changemachine.util;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.util.Log;
import android.view.accessibility.AccessibilityManager;

import java.util.List;

public class AccessibilityUtil {

    /**
     * 获取已安装的无障碍服务列表
     */
    public static List<AccessibilityServiceInfo> getInstalledAccessibilityServices(Context context) {
        AccessibilityManager accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager != null) {
            return accessibilityManager.getInstalledAccessibilityServiceList();
        }
        return null;
    }

    /**
     * 获取已启用的无障碍服务列表
     */
    public static List<AccessibilityServiceInfo> getEnabledAccessibilityServices(Context context) {
        AccessibilityManager accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager != null) {
            return accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
        }
        return null;
    }

    /**
     * 检查某个无障碍服务是否已启用
     */
    public static boolean isAccessibilityServiceEnabled(Context context, String serviceId) {
        AccessibilityManager accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager != null) {
            List<AccessibilityServiceInfo> enabledServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
            for (AccessibilityServiceInfo service : enabledServices) {
                if (serviceId.equals(service.getId())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 打印无障碍服务信息
     */
    public static void printAccessibilityServiceInfo(Context context, List<AccessibilityServiceInfo> services) {
        if (services == null || services.isEmpty()) {
            Log.d("AccessibilityUtil", "没有安装无障碍服务");
            return;
        }

        for (AccessibilityServiceInfo service : services) {
            String serviceId = service.getId(); // 服务 ID
            String serviceName = service.getResolveInfo().loadLabel(context.getPackageManager()).toString(); // 服务名称
            String packageName = service.getResolveInfo().serviceInfo.packageName; // 包名

            Log.d("AccessibilityUtil", "Service: " + serviceName + ", ID: " + serviceId + ", Package: " + packageName);
        }
    }
}